﻿Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            Dim dirs As New ArrayList
            Dim exts As New ArrayList
            Dim names As New ArrayList

            My.Settings.Reload()

            If e.CommandLine.Count > 0 Then
                Try
                    For Each item In My.Settings.prognames
                        names.Add(item)
                        dirs.Add(My.Settings.progdirs(My.Settings.prognames.IndexOf(item)))
                        exts.Add(My.Settings.progexts(My.Settings.prognames.IndexOf(item)))
                    Next

                    Select Case e.CommandLine(0)
                        Case "/L"
                            Try
                                Dim sr As New System.IO.StreamReader(e.CommandLine(1))
                                names.Clear()
                                dirs.Clear()
                                exts.Clear()

                                Do
                                    Try
                                        Dim name As String = ""
                                        Dim dir As String = ""
                                        Dim ext As String = ""
                                        Dim line As String = sr.ReadLine()
                                        Dim i As Integer = 0
                                        Try
                                            Do Until line.Chars(i) = "|"
                                                name += line.Chars(i)
                                                i += 1
                                            Loop
                                            i += 1
                                            Do Until line.Chars(i) = "|"
                                                dir += line.Chars(i)
                                                i += 1
                                            Loop
                                            i += 1
                                            Do Until i = line.Length
                                                ext += line.Chars(i)
                                                i += 1
                                            Loop
                                        Catch
                                            Exit Sub
                                            MsgBox("This file is not valid. It may be incompatible with this version of startup manager. Sorry for any inconvenience")
                                        End Try

                                        names.Add(name)
                                        dirs.Add(dir)
                                        exts.Add(ext)
                                    Catch
                                        Exit Do
                                    End Try
                                Loop
                                sr.Close()
                                My.Settings.prognames = names
                                My.Settings.progdirs = dirs
                                My.Settings.progexts = exts
                                My.Settings.Save()
                                MsgBox("Successfully loaded file")
                            Catch ex As Exception
                                MsgBox(ex.Message)
                            End Try
                            e.Cancel = True

                        Case "/S"
                            Dim sw As New System.IO.StreamWriter(e.CommandLine(1))
                            For Each item In names
                                sw.WriteLine(item & "|" & dirs(names.IndexOf(item)) & "|" & exts(names.IndexOf(item)))
                            Next
                            sw.Close()
                            MsgBox("Successfully saved file")
                            e.Cancel = True

                        Case "/O"
                            StartupOrder.ShowDialog()
                            e.Cancel = True

                        Case "/?"
                            MsgBox("Startup Manager Command Line Args" & vbCr & "/S %path% saves the startup order file to %path%" & vbCr & "/L %path% loads the startup order file at %path%" & "/O shows startup order dialog")
                            e.Cancel = True
                    End Select
                Catch
                    e.Cancel = True
                End Try
            End If
        End Sub
        Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
            MsgBox(e.Exception.Message)
        End Sub
    End Class

End Namespace

